import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import Cookie from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'homeview',
    component: HomeView
  },

  {
    path: '/home',
    name: 'home',
    component: () => import(/* webpackChunkName: "home" */ '../views/HomeView.vue'),
    meta:{title:'首页'},
    children: [
      {
        path: '/manger',
        name: 'manger',
        component: () => import(/* webpackChunkName: "manger" */ '../views/Manger.vue'),
        meta:{title:'资产管理'},
      },
      {
        path:'/main',
        name:'main',
        component:()=>import(/* webpackChunkName: "main" */ '../views/Main.vue'),
      },
      {
        path: '/maintenance',
        name: 'maintenance',
        component: () => import(/* webpackChunkName: "maintenance" */ '../views/Maintenance.vue'),
        meta:{title:'客户管理'},
      },
      {
        path: '/role',
        name: 'role',
        component: () => import(/* webpackChunkName: "role" */ '../views/Role.vue'),
        meta:{title:'系统管理'},
      },
    ]
  },
  {
    path:'/login',
    name:'login',
    component:()=>import(/* webpackChunkName: "login" */ '../views/Login.vue'),
  },
  {
    path: '/revise',
    name: 'revise',
    component: () => import(/* webpackChunkName: "revise" */ '../views/Revise.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import(/* webpackChunkName: "register" */ '../views/Register.vue')
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == "/") {
    let bol = Cookie.get("token")
    let token = bol
    if (token) {
      next()
    } else {
      next("/login")
    }
  } else {
    next()
  }
})

export default router
